package com.gohi.main.wxpay.api.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;




public class WxApi {

	// token 接口
	private static final String TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

	// 创建菜单
	private static final String MENU_CREATE = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";

	// 删除菜单
	private static final String MENU_DELETE = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s";
	private static final String GET_FANS_LIST = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";

	// 获取账号粉丝信息
	private static final String GET_FANS_INFO = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s";
	// 根据Code换openid
	private static final String GET_FANS_CODE = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
	// 网页授权获取粉丝信息
	private static final String GET_FANS_WEB = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
	// 生产二维�?
	private static final String GET_TWO_MA = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";
	// 获取JS授权

	private static final String GET_JS_API = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
	// 发�?模板消息
	private static final String SEND_TEMPLATE_MSG = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";

	// 获取JS授权
	public static String getJsApi(String token) {
		return String.format(GET_JS_API, token);
	}

	/**
	 * 获取模板连接
	 * 
	 * @param token
	 * @return
	 */
	public static String getTemplate(String token) {
		return String.format(SEND_TEMPLATE_MSG, token);
	}

	// 获取二维码请求的地址
	public static String getTwoMa(String token) {
		return String.format(GET_TWO_MA, token);
	}

	// 根据code 换optionId
	public static String getFenCode(String appid, String secret, String code) {
		return String.format(GET_FANS_CODE, appid, secret, code);
	}

	// 获取粉丝的列�?
	public static String getFansList(String access_token, String nexopenid) {
		return String.format(GET_FANS_LIST, access_token, nexopenid);
	}

	public static String getFenWeb(String access_token, String openid) {
		return String.format(GET_FANS_WEB, access_token, openid);
	}

	public static String getTokenUrl(String appId, String appSecret) {
		return String.format(TOKEN, appId, appSecret);
	}

	public static String getMenuCreateUrl(String token) {
		return String.format(MENU_CREATE, token);
	}

	public static String getMenuDeleteUrl(String token) {
		return String.format(MENU_DELETE, token);
	}

	public static String getFansInfoUrl(String token, String openid) {
		return String.format(GET_FANS_INFO, token, openid);
	}


	// 发�?请求
	public static JSONObject httpsRequest(String requestUrl,
			String requestMethod, String outputStr) {
		JSONObject jsonObject = null;
		try {
			TrustManager[] tm = { new JEEWeiXinX509TrustManager() };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());
			SSLSocketFactory ssf = sslContext.getSocketFactory();

			URL url = new URL(requestUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.setSSLSocketFactory(ssf);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(requestMethod);
			if (null != outputStr) {
				OutputStream outputStream = conn.getOutputStream();
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}
			InputStream inputStream = conn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(
					inputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(
					inputStreamReader);
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			inputStream.close();
			inputStream = null;
			conn.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

}

class JEEWeiXinX509TrustManager implements X509TrustManager {
	public void checkClientTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
	}

	public void checkServerTrusted(X509Certificate[] chain, String authType)
			throws CertificateException {
	}

	public X509Certificate[] getAcceptedIssuers() {
		return null;
	}
}